package com.rui.common.util;

import com.rui.common.exception.MyException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * 校验工具类
 *
 * @author xr
 * @email xr@qq.com
 * @date 2020-01-01 00:00:00
 */
public class ValidatorUtil {
    private static Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    /**
     * 字符串非空
     *
     * @param str
     * @param message
     */
    public static void isBlank(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new MyException(message);
        }
    }

    /**
     * 字符串非空
     *
     * @param str
     * @param message
     */
    public static void isNotBlank(String str, String message) {
        if (StringUtils.isNotBlank(str)) {
            throw new MyException(message);
        }
    }

    /**
     * 数组非空
     *
     * @param array
     * @param message
     */
    public static void isEmpty(Object[] array, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new MyException(message);
        }
    }

    /**
     * 集合非空
     *
     * @param collection
     * @param message
     */
    public static void isEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new MyException(message);
        }
    }

    /**
     * 集合非空
     *
     * @param collection
     * @param message
     */
    public static void isNotEmpty(Collection<?> collection, String message) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new MyException(message);
        }
    }

    /**
     * map非空
     *
     * @param map
     * @param message
     */
    public static void isEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(Collections.singleton(map))) {
            throw new MyException(message);
        }
    }

    /**
     * 对象非空
     *
     * @param object
     * @param message
     */
    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new MyException(message);
        }
    }

    /**
     * 对象非空
     *
     * @param object
     * @param message
     */
    public static void isNotNull(Object object, String message) {
        if (object != null) {
            throw new MyException(message);
        }
    }

    /**
     * 对象非空
     *
     * @param index
     * @param message
     */
    public static void checkIndex(int index, String message) {
        if (index == 0) {
            throw new MyException(message);
        }
    }

    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @param groups 待校验的组
     * @throws MyException 校验不通过，则报MyException异常
     */
    public static void validateEntity(Object object, Class<?>... groups) throws MyException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
            throw new MyException(constraint.getMessage());
        }
    }

}
